package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.SetmealDishMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.domain.Category;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SetmealServiceImpl
        extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;




    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //保存套餐数据，seteal到 seteamlmapper 中
        setmealMapper.insert(setmealDto);

        //保存套餐菜品数据到 setmeal_dish中
        //拿到菜品列表
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //拿到套餐id
        Long id = setmealDto.getId();
        //遍历,拿到套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //设置值
            setmealDish.setSetmealId(id);
        }
        //保存菜品数据
        setmealDishService.saveBatch(list);


    }

    @Override
    public void delectSetmeal(List<Long> ids) {
        //套餐是售卖（启用）状态，如果是，不能删
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //按照id和status删除
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(wrapper);

        if(count > 0){
            //套餐在售卖状态，不能删除
            throw new CustomException("当前套餐正在售卖，不能删除");
        }
        setmealMapper.deleteBatchIds(ids);
        //按照id删除套餐
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();

        wrapper1.in(SetmealDish::getId,ids);
        setmealDishMapper.delete(wrapper1);

        //删除套餐对应的菜品数据



    }

    /**
     * 状态修改
     * @param status
     * @param ids
     */
    @Override
    public void statusSetmeal(Integer status, List<Long> ids) {
        //遍历
        for (Long id : ids) {
            //初始化
            Setmeal setmeal = new Setmeal();
            //修改至
            setmeal.setStatus(status);
            setmeal.setId(id);
            //修改值
            setmealMapper.updateById(setmeal);
        }

    }

    /**
     * 更新套餐数据保存
     * @param setmealDto
     */
    @Override
    public void updateSave(SetmealDto setmealDto) {
        //更新套餐数据
        setmealMapper.updateById(setmealDto);
        //更新套餐菜品数据
          //先根据套餐id删除套餐对应菜品的数据
        LambdaQueryWrapper<SetmealDish> wapper = new LambdaQueryWrapper<>();
        wapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishMapper.delete(wapper);
          //重新添加新的套餐对应数据

        //拿到套餐id
        Long id = setmealDto.getId();
        //删完之后继续添加
        //套餐菜品全部数据
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //遍历，套餐数据
        for (SetmealDish setmealDish : list) {
            //需要塞一个数据
            setmealDish.setSetmealId(id);
            //新增菜品数据
            setmealDishMapper.insert(setmealDish);
        }

    }






}